package com.yvan.metric;

import com.yvan.platform.JsonWapper;

import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicLongArray;

/**
 * Created by xiaoEight on 2018/8/13.
 */
public class Histogram {

    private final long[] ranges;
    private final AtomicLongArray rangeCounters;
    private final AtomicLong sum = new AtomicLong();
    private long max = 0L;
    private long min = 0L;

    public Histogram(long... ranges) {
        this.ranges = ranges;
        this.rangeCounters = new AtomicLongArray(ranges.length + 1);
    }

    public static long[] mergeArray(Object h1o, Object h2o) {
        long[] h1 = (long[]) h1o;
        long[] h2 = (long[]) h2o;

        long[] r = new long[h1.length];
        r[0] = Math.max(h1[0], h2[0]);
        r[1] = Math.min(h1[1], h2[1]);
        for (int i = 2; i < h1.length; i++) {
            r[i] = h1[i] + h2[i];
        }
        return r;
    }

    public static void writeToMap(JsonWapper jw, String key, long[] cc1) {
        if (cc1 == null) {
            return;
        }
        jw.set(key + "_max", cc1.length >= 1 ? cc1[0] : 0);
        jw.set(key + "_min", cc1.length >= 2 ? cc1[1] : 0);
        jw.set(key + "_sum", cc1.length >= 3 ? cc1[2] : 0);
        jw.set(key + "_cnt", cc1.length >= 4 ? cc1[3] : 0);
        if (jw.contains(key + "_sum") && jw.contains(key + "_cnt")) {
            if (cc1[3] > 0) {
                jw.set(key + "_avg", cc1[2] / cc1[3]);
            } else {
                jw.set(key + "_avg", 0);
            }
        }
        jw.set(key + "_0", cc1.length >= 5 ? cc1[4] : 0);
        jw.set(key + "_1", cc1.length >= 6 ? cc1[5] : 0);
        jw.set(key + "_2", cc1.length >= 7 ? cc1[6] : 0);
        jw.set(key + "_3", cc1.length >= 8 ? cc1[7] : 0);
        jw.set(key + "_4", cc1.length >= 9 ? cc1[8] : 0);
        jw.set(key + "_5", cc1.length >= 10 ? cc1[9] : 0);
        jw.set(key + "_6", cc1.length >= 11 ? cc1[10] : 0);
        jw.set(key + "_m", cc1.length >= 12 ? cc1[11] : 0);
    }

    @SuppressWarnings("unchecked")
    public static void writeToMap(Map map, String key, long[] cc1) {
        if (cc1 == null) {
            return;
        }
        map.put(key + "_max", cc1.length >= 1 ? cc1[0] : 0);
        map.put(key + "_min", cc1.length >= 2 ? cc1[1] : 0);
        map.put(key + "_sum", cc1.length >= 3 ? cc1[2] : 0);
        map.put(key + "_cnt", cc1.length >= 4 ? cc1[3] : 0);
        if (map.containsKey(key + "_sum") && map.containsKey(key + "_cnt")) {
            if (cc1[3] > 0) {
                map.put(key + "_avg", cc1[2] / cc1[3]);
            } else {
                map.put(key + "_avg", 0);
            }
        }
        map.put(key + "_0", cc1.length >= 5 ? cc1[4] : 0);
        map.put(key + "_1", cc1.length >= 6 ? cc1[5] : 0);
        map.put(key + "_2", cc1.length >= 7 ? cc1[6] : 0);
        map.put(key + "_3", cc1.length >= 8 ? cc1[7] : 0);
        map.put(key + "_4", cc1.length >= 9 ? cc1[8] : 0);
        map.put(key + "_5", cc1.length >= 10 ? cc1[9] : 0);
        map.put(key + "_6", cc1.length >= 11 ? cc1[10] : 0);
        map.put(key + "_m", cc1.length >= 12 ? cc1[11] : 0);
    }

    public void reset() {
        for (int i = 0; i < rangeCounters.length(); i++) {
            rangeCounters.set(i, 0);
        }
    }

    public void record(long millis) {

        sum.addAndGet(millis);
        if (millis > max) {
            max = millis;
        }
        if (millis < min) {
            min = millis;
        }

        int index = rangeCounters.length() - 1;
        for (int i = 0; i < ranges.length; ++i) {
            if (millis < ranges[i]) {
                index = i;
                break;
            }
        }

        rangeCounters.incrementAndGet(index);
    }

    public long get(int index) {
        return rangeCounters.get(index);
    }

    public long[] toArray() {
        long[] array = new long[rangeCounters.length() + 4];
        array[0] = max;
        array[1] = min;
        array[2] = sum.get();
        array[3] = getSum();

        for (int i = 0; i < rangeCounters.length(); i++) {
            array[i + 4] = rangeCounters.get(i);
        }
        return array;
    }

    public long[] toArrayAndReset() {
        long[] array = new long[rangeCounters.length() + 4];
        array[0] = max;
        array[1] = min;
        array[2] = sum.get();
        array[3] = getSum();

        for (int i = 0; i < rangeCounters.length(); i++) {
            array[i + 4] = rangeCounters.getAndSet(i, 0);
        }
        return array;
    }

    public long[] getRanges() {
        return ranges;
    }

    public long getValue(int index) {
        return rangeCounters.get(index);
    }

    public long getSum() {
        long sum = 0;
        for (int i = 0; i < rangeCounters.length(); ++i) {
            sum += rangeCounters.get(i);
        }
        return sum;
    }

    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append('[');
        for (int i = 0; i < rangeCounters.length(); ++i) {
            if (i != 0) {
                buf.append(',');
            }
            buf.append(rangeCounters.get(i));
        }
        buf.append(']');
        return buf.toString();
    }

    /*
    public void merge(Histogram histogram) {
        for (int i = 0; i < rangeCounters.length(); ++i) {
            this.rangeCounters.addAndGet(i, histogram.get(i));
        }
    }

    public Histogram copy() {
        Histogram copy = new Histogram(this.ranges);
        for (int i = 0; i < rangeCounters.length(); ++i) {
            copy.rangeCounters.set(i, this.get(i));
        }
        return copy;
    }
    */
}
